package com.zmn.plat.business.impl.channel;

import javax.annotation.Resource;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.channel.ChannelOrderConfigBService;
import com.zmn.plat.business.interfaces.channel.ChannelWorkOrderVideoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.channel.ChannelWorkOrderTypeDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.dictionary.ChannelDict;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.order.config.ChannelOrderConfig;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import com.zmn.plat.services.interfaces.channel.order.config.ChannelOrderConfigService;
import com.zmn.plat.services.interfaces.channel.workorder.picture.ChannelWorkOrderPictureService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 描述:ChannelTypeBServiceImpl
 *
 * @author tanghongjun
 * @since: 2018/10/16 17:56
 */
@Service
public class ChannelOrderConfigBServiceImpl implements ChannelOrderConfigBService {
    @Resource
    private ChannelOrderConfigService channelOrderConfigService;

    @Resource
    private ChannelService channelService;

    @Resource
    private ChannelWorkOrderPictureService channelWorkOrderPictureService;

    @Resource
    private ChannelWorkOrderVideoBService channelWorkOrderVideoBService;

    @Override
    public ChannelWorkOrderTypeDRO getWorkOrderTypeByChannelId(Integer channelId) {
        if (NumberUtil.isNullOrZero(channelId)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ChannelOrderConfig channelOrderConfig = channelOrderConfigService.findByKey(channelId);
        if (Objects.isNull(channelOrderConfig)) {
            Channel channel = channelService.findByKey(channelId);

            if (Objects.nonNull(channel) && Objects.equals(ChannelDict.RANK_TWO, channel.getRank()) && NumberUtil.isPositiveInteger(channel.getParentId())) {
                channelOrderConfig = channelOrderConfigService.findByKey(channel.getParentId());
            }
        }

        ChannelWorkOrderTypeDRO result = new ChannelWorkOrderTypeDRO();
        if (Objects.nonNull(channelOrderConfig)) {
            result.setWorkOrderCancelType(channelOrderConfig.getWorkOrderCancelType());
            result.setWorkOrderCompleteType(channelOrderConfig.getWorkOrderCompleteType());
            result.setWorkOrderSpecialOfferType(channelOrderConfig.getWorkOrderSpecialOfferType());
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(Integer originalChannelId, Integer targetChannelId, String operator) {

        // 渠道订单配置
        channelOrderConfigService.deleteByKey(targetChannelId);
        channelOrderConfigService.copy(originalChannelId, targetChannelId, operator);
        // 复制工单照片
        channelWorkOrderPictureService.deleteByChannelId(targetChannelId);
        channelWorkOrderPictureService.copy(originalChannelId, targetChannelId, operator);
        // 复制工单视频
        channelWorkOrderVideoBService.deleteByChannelId(targetChannelId);
        channelWorkOrderVideoBService.copy(originalChannelId, targetChannelId, operator);
    }
}
